package com.net.test.controller;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.net.test.service.BoardService;
import com.net.test.service.ExboardService;
import com.net.test.vo.ExboardVO;

@Controller
public class ExboardController {
	private static final Logger logger = LoggerFactory.getLogger(BoardService.class);
	
	@Autowired
	ExboardService exboardService;
	@RequestMapping(value = "/exList.ncb", method = {RequestMethod.GET,RequestMethod.POST})
    public String exList(Locale locale, Model model) throws Exception{
		
		ExboardVO tvo = new ExboardVO();
		
		List<ExboardVO> list  = (List<ExboardVO>)exboardService.selectData("testMapper.selectExBoard");
		ExboardVO view = (ExboardVO)exboardService.selectData("testMapper.exPage");
		model.addAttribute("list", list);
		model.addAttribute("view", view);
		
		return "exList";
		
		
	}
	@RequestMapping(value ="/exWrite.ncb",method = {RequestMethod.GET, RequestMethod.POST})
	public String exWrite(Locale locale, Model model)throws Exception{
		
		return "exWrite";
	}
	@RequestMapping(value = "/exInsert.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String exInsert(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		
		ExboardVO insert = new ExboardVO();
		
		String title = request.getParameter("title");
		String contents = request.getParameter("contents");
		//String exNm	= "게시판";
		
		insert.setBoard_Title(title);
		insert.setBoard_Contents(contents);
		//insert.setBoard_Name(exNm);
		
		int result = exboardService.insertData("testMapper.exaddBoard", insert);
		
		List<ExboardVO> list = (List<ExboardVO>)exboardService.selectData("testMapper.selectExBoard");
		
		model.addAttribute("list", list);
		
		return "exList";
		
	}
	@RequestMapping(value = "exView.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String exView(Locale locale, Model model, HttpServletRequest request)throws Exception{		
		
		String title = request.getParameter("title");
		
		ExboardVO view  = (ExboardVO)exboardService.getViewData("testMapper.exView",title);
		
		model.addAttribute("view", view);
		
		return "exView";		
		
	}
	@RequestMapping(value = "exEdit.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String exEdit(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		String title = request.getParameter("edit1");
		String contents = request.getParameter("edit2");
		
		ExboardVO edit = new ExboardVO();
		
		edit.setBoard_Title(title);
		edit.setBoard_Contents(contents);
		
		model.addAttribute("edit",edit);
		
		return "exEdit";
	}
	@RequestMapping(value ="exUpdate.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String exUpedate(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		String title = request.getParameter("uptitle");
		String contents = request.getParameter("contents");
		
		
		ExboardVO update = new ExboardVO();
		
		update.setBoard_Title(title);
		update.setBoard_Contents(contents);
		
		
		int result = exboardService.updateData("testMapper.exUpdate", update);
		
		List<ExboardVO> list = (List<ExboardVO>)exboardService.selectData("testMapper.selectExBoard");
		
		model.addAttribute("list",list);
		
		return "exList";
		
	}
	@RequestMapping(value ="exDelete.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String exDelete(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		String dtitle = request.getParameter("del");
		
		
		int result = exboardService.deleteData("testMapper.exDelete",dtitle);
		
		List<ExboardVO> list = (List<ExboardVO>)exboardService.selectData("testMapper.selectExBoard");
		
		model.addAttribute("list", list);
		
		return "exList";
	}
	
}
